import {
  request,
  requestPay,
  validator
} from '../../utils/requestUtils';

import { userBehavior } from '../../behaviors/userBehavior'

import {
  formatTime,
} from '../../utils/util';

Page({
  behaviors: [userBehavior],
  data: {
    options: '',
    minHour: new Date().getHours,
    show: false, // 期望送达日期弹框
    deliveryTimeshow: false,// 期望送达时间点
    minDate: new Date().getTime(),
    currentDate: new Date().getTime(),
    form: {
      customerName: '',
      contact: '',
      payableAmount: '0',
      deliveryDate: '',//送达日期
      deliveryStr: '',//送达时间点
      remark: '',
      addrId: '',//地址id
      lines: [],//行信息
    },
    addr: {
      id: '',
    }
  },
  onShow() {
   
  },
  onLoad(options) {
    if (options.id) {
      this.getOrderById(options.id)
    } else {
      this.getOrderBycustomerOrderId(options.orderId)
    }
  },

  // 选择退货原因
  sekectReturnType() {
    if (this.data.form.orderStatus != 0) return
    let component = this.selectComponent('#dictCon')
    if (component) {
      component.addhow('return_type');
    }
  },
  // 数据字典返回
  dictConfirm(e) {
    if (e.detail.key == "return_type") {
      this.setData({
        'form.returnType': e.detail.dictValue,
        'form.returnTypeName': e.detail.dictLabel,
      })
    }
  },



  //获取退货单信息
  async getOrderById(id) {
    const result = await request({
      url: `/bus/returnOrder/api/${id}`,
      method: "GET"
    })
    if (result.data.code == 200) {
      this.setData({
        form: result.data.data
      })
    }
  },
  // 根据商家订单id查询退货单信息
  async getOrderBycustomerOrderId(orderId) {
    const result = await request({
      url: `/bus/returnOrder/details`,
      method: "POST",
      data: {
        orderId: orderId
      }
    })
    if (result.data.code == 200) {
      this.setData({
        form: result.data.data
      })
      if (result.data.data.fileIds) {
        this.selectComponent('#listUploader').initListHandler(result.data.data.fileIds.split(',').map(v => {
          return {
            url: v
          }
        }))
      }
    }
  },
  // 提交订单
  async submitOrder() {
    console.log('this.data.form', this.data.form)
    let postMpdel = JSON.parse(JSON.stringify(this.data.form))
    const { valid } = await this.validatorAddress(postMpdel)
    let picList = this.selectComponent('#listUploader').data.fileList
    if (picList && picList.length > 0) {
      this.setData({
        'form.fileIds': picList.map(v => v.url).join(',')
      })
    }
    console.log('picList', picList);
    if (!valid) return
    const result = await request({
      url: `/bus/returnOrder`,
      method: "POST",
      data: this.data.form
    })
    if (result.data.code == 200) {
      wx.navigateTo({
        url: '/pages/customer_return_order_list/index',
      })
    }
  },
  // 数据校验
  validatorAddress(params) {
    console.log('params', params)
    const rules = {
      returnType: [
        { required: true, message: '退货原因不能为空' },
      ]
    }
    return validator(params, rules)
  },
  // 设置订单备注
  onTextAreaChange(e) {
    this.setData({
      'form.remark': e.detail.value
    })
  }
})
